package com.util;

import java.sql.*;

public class RequestProductViewData {
    //1.准备连接对象
    Connection conn;
    //2.准备编译对象
    PreparedStatement ps;

    public String SelectCategoryName(int ProductCategoryID) {
        //3.开始连接数据库
        conn = BaseDao.getConn();
        //4.准备SQL语句
        String sql = "select CategoryName from ProductView where ProductCategoryID = ?";
        //5.编译SQL语句
        try {
            ps = conn.prepareStatement(sql);
            //6.设置？的值
            ps.setInt(1, ProductCategoryID);

            //7.执行SQL语句
            ResultSet rs  = ps.executeQuery(); //返回查询后的结果
            if (rs.next()) {
                String categoryName = rs.getString("CategoryName");
                return categoryName;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            //关闭coon
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //关闭ps
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(new RequestProductViewData().SelectCategoryName(1002));
    }
}
